import Vue from 'vue';
import Vuex from 'vuex';
Vue.use(Vuex);
//定义状态
const state = {
	token: null,
    pageStack: [],
    account: null,
    loginParam: null
};
//定义改变状态的方法
const mutations = {
	setAccount(state, account) {
		if (account != null) {
			localStorage.setItem('account', JSON.stringify(account));
			localStorage.setItem('account-token', account.token);
			state.account = account;
			state.token = account.token;
		}
	},
	setLoginParam(state, param) {
		if (param != null) {
			localStorage.setItem('login-param-name',  param.phone);
			localStorage.setItem('login-param-pwd',  param.password);
		}
	},

	clearLoginParam(state, phone) {
		console.log("phone");
		localStorage.removeItem('login-param-pwd');
		localStorage.setItem('login-param-name',  phone);
	}
}
const getters = {
	loginParam: (state) => {
		state.loginParam = {
			phone: localStorage.getItem('login-param-name'),
			password: localStorage.getItem('login-param-pwd'),
		}
		console.log("loginParam", state.loginParam);
		return state.loginParam;
	},
	token: (state) => {
		if (state.token == null || state.token == 'undefined') {
			console.log("state.token", state.token)
			var accStr = localStorage.getItem('account');
			if (accStr != null) {
				var account = JSON.parse(accStr);
				state.account = account;
				state.token = localStorage.getItem('account-token');
				console.log(state.token);
			}
		}
		return state.token;
	},
	account: (state) => {
		if (state.account == null) {
			var accStr = localStorage.getItem('account');
			if (accStr != null) {
				var account = JSON.parse(accStr);
				state.account = account;
				state.token = localStorage.getItem('account-token');
			}
		}
		return state.account;
	}
}
export default new Vuex.Store({
    state, mutations, getters
});
